#include "mettingwidget.h"
#include <QEvent>
#include <QGroupBox>
#include <QLabel>
#include <QPaintEvent>
#include <QPainter>
#include "screen.h"
#include "utils/util.h"
#include <sstream>

float FixedToolbarSize = 60;
float buttonPart = (9.0f / 20); //这是存放四个按钮的groupBox的比例
float dateGroup = (11.0f / 20);
float mettingIconWidth = (65.0f / 785);
float mettingIconHeight = (60.0f / 480);

float logoPixWidthRatio = (409.0f / 785);
float logoPixHeightRatio = (230.0f / 480);

MettingWidget::MettingWidget(QWidget *parent)
    : FrameLessRoundedWindow(parent)
    , mainHLayout(new QHBoxLayout(this))
    , toolGroupBox(new ToolBarGroupBox())
    , rightFrameGouupBox(new RightFrameGroupBox())
    , dategroupBox(new dateGroupBox())
    , closeBtn(new QToolButton())
    , minBtn(new QToolButton())
    , maxRecoverBtn(new QToolButton())
    , videoMetting(new QToolButton())
    , joinMetting(new QToolButton())
    , createMetting(new QToolButton())
    , reserveMetting(new QToolButton())
    , mettingTool(new QToolButton())
{
    //启动鼠标追踪
    this->setMouseTracking(true);
    mettingWidgetRect = this->rect();
    qDebug() << mettingWidgetRect.width();
    qDebug() << mettingWidgetRect.height();
    mainHLayout->setObjectName("mainHLayout");
    mainHLayout->setContentsMargins(0, 0, 0, 0);
    mainHLayout->setAlignment(Qt::AlignCenter);
    mainHLayout->setSpacing(0);

    remainderWidth = (mettingWidgetRect.width()) - FixedToolbarSize;
    //qDebug() << FixedToolbarSize + buttonPart * remainderWidth + dateGroup * remainderWidth;
    //添加第一个groupBox的
    addGroupBoxToWidget(toolGroupBox,
                        "toolGroupBox",
                        "QGroupBox{background-color: rgb(240, 240, 240);}");
    toolGroupBox->setMaximumWidth(FixedToolbarSize); //设置这个的最大宽度
    toolGroupBox->resize(FixedToolbarSize, mettingWidgetRect.height());
    qDebug() << "toolGroupBox size :" << toolGroupBox->size();
    // // //这是放在groupBox中的布局
    QVBoxLayout *vboxLayout = new QVBoxLayout();
    vboxLayout->setContentsMargins(10, 10, 10, 10);
    setLayout(vboxLayout, "vboxLayout", Qt::AlignCenter, 15);
    //添加控件
    addGroupInternalComponents(
        vboxLayout,
        videoMetting,
        "QToolButton{background-color:rgb(240, 240, 240);color:#1F71D1;border:none;}",
        "videoMetting",
        "会议",
        ":/resources/Metting.png",
        QSize(25, 25));

    toolGroupBox->setLayout(vboxLayout);
    // // //加入第二个

    addGroupBoxToWidget(rightFrameGouupBox,
                        "rightFrameGouupBox",
                        "QGroupBox{background-color: white;border:none;}");
    rightFrameGouupBox->resize(QSize(buttonPart * remainderWidth + 200, mettingWidgetRect.height()));

    QHBoxLayout *HboxLayout1 = new QHBoxLayout();
    setLayout(HboxLayout1, "HboxLayout1", Qt::AlignCenter, 25);

    QVBoxLayout *vboxLayout2 = new QVBoxLayout();
    setLayout(vboxLayout2, "vboxLayout2", Qt::AlignCenter, 25);
    //加入会议按钮
    addGroupInternalComponents(HboxLayout1,
                               joinMetting,
                               "QToolButton{background-color:white;border:none;font-size:14px;}",
                               "joinMetting",
                               "加入会议",
                               ":/resources/add-user.png",
                               QSize(65, 60));

    addGroupInternalComponents(HboxLayout1,
                               createMetting,
                               "QToolButton{background-color:white;border:none;font-size:14px;}",
                               "joinMetting",
                               "创建会议",
                               ":/resources/create.png",
                               QSize(65, 60));

    vboxLayout2->addLayout(HboxLayout1);

    // //创建会议按钮，但是这两个按钮都是放在一个水平布局中的

    //继续加按钮
    QHBoxLayout *HboxLayout2 = new QHBoxLayout();
    setLayout(HboxLayout2, "HboxLayout2", Qt::AlignCenter, 25);

    addGroupInternalComponents(HboxLayout2,
                               reserveMetting,
                               "QToolButton{background-color:white;border:none;font-size:14px;}",
                               "reserveMetting",
                               "预约会议",
                               ":/resources/yuyue.png",
                               QSize(65, 60));

    addGroupInternalComponents(HboxLayout2,
                               mettingTool,
                               "QToolButton{background-color:white;border:none;font-size:14px;}",
                               "mettingTool",
                               "会议工具",
                               ":/resources/metting_tool.png",
                               QSize(65, 60));

    vboxLayout2->addLayout(HboxLayout2);

    rightFrameGouupBox->setLayout(vboxLayout2);
    qDebug() << "rightFrameGouupBox size:" << rightFrameGouupBox->size();
    qDebug() << QSize(buttonPart * remainderWidth, mettingWidgetRect.height());

    addGroupBoxToWidget(dategroupBox, "groupBox3", "QGroupBox{background-color: white}");
    dategroupBox->resize(QSize(dateGroup * remainderWidth, mettingWidgetRect.height()));

    dategroupBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
    qDebug() << dategroupBox->size();
    qDebug() << dateGroup * remainderWidth;
    // // //添加一个垂直布局
    QVBoxLayout *vBoxLayout31 = new QVBoxLayout();
    vBoxLayout31->setContentsMargins(10, 10, 10, 10);
    setLayout(vBoxLayout31, "vBoxLayout31", Qt::AlignCenter, 0);

    addMinMaxCloaseBtn(vBoxLayout31);

    //垂直布局添加到groupBox3中去
    //在这里再加一个弹簧
    vBoxLayout31->addStretch();
    //添加日期的
    tm local_time = getNowTime();
    addTimeLabel(local_time, vBoxLayout31);
    vBoxLayout31->addStretch();
    // //添加一张照片
    logLabel = new QLabel(dategroupBox);
    logLabel->setObjectName("logLabel");
    logLabel->setAlignment(Qt::AlignCenter);
    //logLabel->setStyleSheet(R"(QLabel{border:1px solid gray;})");
    setLogoPix(QSize(dateGroup * remainderWidth, mettingWidgetRect.height() / 2));
    vBoxLayout31->addWidget(logLabel);

    dategroupBox->setLayout(vBoxLayout31);
    bindConnect();
}

void MettingWidget::addGroupBoxToWidget(QGroupBox *groupBox,
                                        const QString &groupObjName,
                                        const QString &style)
{
    groupBox->setObjectName(groupObjName);
    groupBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    groupBox->setTitle("");
    groupBox->setStyleSheet(style);
    mainHLayout->addWidget(groupBox);
}

void MettingWidget::addMinMaxCloaseBtn(QBoxLayout *parentLayout)
{
    QHBoxLayout *hLayout = new QHBoxLayout();
    hLayout->setObjectName("hLayout");
    hLayout->setAlignment(Qt::AlignCenter);
    hLayout->setSpacing(0);
    hLayout->addStretch(); //添加一个弹簧使其到最右端
    setQToolButtonProperty(closeBtn, "closeBtn", "", ":/resources/close_20.svg");
    setQToolButtonProperty(minBtn, "minBtn", "", ":/resources/minimize_20.svg");
    setQToolButtonProperty(maxRecoverBtn, "maxRecoverBtn", "", ":/resources/maximize_20_normal.svg");

    hLayout->addWidget(minBtn);
    hLayout->addWidget(maxRecoverBtn);
    hLayout->addWidget(closeBtn);

    //然后就加入到父布局中
    parentLayout->addLayout(hLayout);
}

void MettingWidget::addGroupInternalComponents(QBoxLayout *boxLayout,
                                               QToolButton *toolButton,
                                               const QString &toolStyle,
                                               const QString &toolObjectName,
                                               const QString &toolName,
                                               const QString &iconPath,
                                               QSize iconSize)
{
    //新加会议这个按钮
    toolButton->setObjectName(toolObjectName);
    toolButton->setText(toolName);
    toolButton->setStyleSheet(toolStyle);
    toolButton->setIcon(QIcon(iconPath));
    toolButton->setIconSize(iconSize);
    toolButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    boxLayout->addWidget(toolButton);
}

void MettingWidget::setLayout(QBoxLayout *boxLayout,
                              const QString &ObjName,
                              Qt::Alignment align,
                              int spacing)
{
    boxLayout->setObjectName(ObjName);
    boxLayout->setAlignment(align);
    boxLayout->setSpacing(spacing);
}

void MettingWidget::setQToolButtonProperty(QToolButton *btn,
                                           const QString &objectName,
                                           const QString &text,
                                           const QString &iconFilePath)
{
    btn->setObjectName(objectName);
    btn->setIcon(QIcon(iconFilePath));
    btn->setText(text);
    btn->setStyleSheet(R"(QToolButton{border:none;}
                                    QToolButton:hover{ background-color:rgb(211, 211, 211);})");
}

tm MettingWidget::getNowTime()
{
    time_t timeStamp = time(nullptr); //获取当前的时间戳
    struct tm localTime;
    localtime_s(&localTime, &timeStamp);
    return localTime;
}

void MettingWidget::addTimeLabel(const tm &local_time, QBoxLayout *boxLayout)
{
    timeLabel = new QLabel();
    timeLabel->setObjectName("timeLabel");
    timeLabel->setAlignment(Qt::AlignCenter);
    timeLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    std::stringstream ss;
    ss << std::to_string(local_time.tm_mon) << "月" << std::to_string(local_time.tm_mday) << "日";
    QString time = QString::fromStdString(ss.str());
    timeLabel->setText(time);
    setLable(timeLabel, "timeLabel", time, "Microsoft YaHei", 20);
    // timeLabel->setStyleSheet(R"(QLabel{border:1px solid geay;color: rgb(0, 0, 0);})");
    QFont font = timeLabel->font();
    font.setBold(true);
    timeLabel->setFont(font);
    boxLayout->addWidget(timeLabel);
}

void MettingWidget::bindConnect()
{
    connect(closeBtn, &QToolButton::clicked, [this]() { this->close(); });
    connect(minBtn, &QToolButton::clicked, [this]() {
        this->setWindowState(windowState() & ~Qt::WindowActive | Qt::WindowMinimized);
    });

    //Qt::WindowNoState 表示窗口是初始化状态，没有任何其它的状态
    //testFlag()是一个成员函数，用于测试指定的标志位是否被设置
    //检查窗口是否处于初始化状态（正常状态），如果是则是需要设置为最大化的状态。如果不是正常状态则说明是最大化的状态
    //则变为正常的状态Qt::WindowNoState就是正常的状态。
    connect(maxRecoverBtn, &QToolButton::clicked, [this]() {
        this->setWindowState(windowState().testFlag(Qt::WindowNoState) ? Qt::WindowMaximized
                                                                       : Qt::WindowNoState);
    });
}

void MettingWidget::changeGroupSize(QGroupBox *group, int width)
{
    group->setMinimumWidth(width);
    group->setMaximumWidth(width);
}

void MettingWidget::changeBtnSize(QSize size)
{
    joinMetting->setIconSize(size);
    createMetting->setIconSize(size);
    reserveMetting->setIconSize(size);
    mettingTool->setIconSize(size);
}

void MettingWidget::setLogoPix(QSize size)
{
    QPixmap pixmap(":/resources/logo.svg");
    QPixmap scalePixmap = pixmap.scaled(size, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    logLabel->setPixmap(scalePixmap);
}

/**
 * @brief MettingWidget::changeEvent  当窗体发生变化时就会触发这个事件
 * @param event  这里一般发生的事件就是窗口变化事件
 */
void MettingWidget::changeEvent(QEvent *event)
{
    QRect rect = this->rect();
    rect.setRect(rect.x(), rect.y(), rect.width() - FixedToolbarSize, rect.height());
    if (event->type() == QEvent::WindowStateChange) {
        //窗口变化事件之后，重新初始化一下
        Screen::init();
        //需要判断是什么事件，是最大话还是正常化,通过判断这个标志即可，testFlag就是判断标识位是否被设置如果被设置了，则说明是正常状态的
        if (this->windowState().testFlag(Qt::WindowNoState)) {
            //这里就是重新设置了
            changeGroupSize(rightFrameGouupBox, buttonPart * rect.width());
            changeGroupSize(dategroupBox, dateGroup * rect.width());
            changeBtnSize(QSize(mettingIconWidth * rect.width(), mettingIconHeight * rect.height()));
            maxRecoverBtn->setIcon(QIcon(":/resources/maximize_20_normal.svg"));
            setLogoPix(QSize(dateGroup * rect.width(), rect.height() / 2));
            FrameLessRoundedWindow::childWidgetMaxed = false;
        } else if (this->windowState().testFlag(Qt::WindowMaximized)) {
            changeGroupSize(rightFrameGouupBox, buttonPart * rect.width());
            changeGroupSize(dategroupBox, dateGroup * rect.width());
            changeBtnSize(QSize(mettingIconWidth * rect.width(), mettingIconHeight * rect.height()));
            maxRecoverBtn->setIcon(QIcon(":/resources/maximize_20_max.svg"));
            qDebug() << logLabel->size();
            setLogoPix(QSize(dateGroup * rect.width(), rect.height() / 2));
            FrameLessRoundedWindow::childWidgetMaxed = true;
        }
    }
}

void MettingWidget::resizeEvent(QResizeEvent *event)
{
    QRect rect = this->rect();
    rect.setWidth(rect.width() - FixedToolbarSize);
    changeGroupSize(rightFrameGouupBox, buttonPart * rect.width());
    changeGroupSize(dategroupBox, dateGroup * rect.width());
    changeBtnSize(QSize(mettingIconWidth * rect.width(), mettingIconHeight * rect.height()));
    setLogoPix(QSize(dateGroup * rect.width(), rect.height() / 2));
}
